package com.ssyshop.info.https;

import com.ssyshop.info.utils.Config;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by xwc on 2017/5/23.
 */
public class OkHttpClientManager {

    private static OkHttpClient mOkHttpClient;

    //普通参数post请求
    public static Call sendPost(FormBody.Builder params,String url) {

        mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        Request requestPost = new Request.Builder()
                                .url(Config.URL_SERVER+url)
                                .post(params.build())
                                .build();
        return mOkHttpClient.newCall(requestPost);
    }

    public static String sendGet(String url){
        mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        String res=null;


        try {
            Response response=mOkHttpClient.newCall(request).execute();
            res=response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            res=null;
        }

        return res;
    }


}
